package com.whzm.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;

@Configuration
public class DelayQueueConfig implements Ordered {


    @Override
    public int getOrder() {
        return -100;
    }
    //延时队列名字
    public final static String DELAY_QUEUE_NAME="delay_queue_ttl";
    //工作队列名字
    public final static String DELAY_PROCESS_QUEUE_NAME="delay_process_queue";
    //死信交换机名字
    public final static String DELAY_EXCHANGE_NAME="delay_exchange";
    //全局过期时间
    public final static int DELAY_EXPIRATION=4000;

    //创建接收消息的延时队列
    @Bean
    Queue delayQueuePerQueueTTl(){
        return QueueBuilder.durable(DELAY_QUEUE_NAME)
                .withArgument("x-dead-letter-exchange",DELAY_EXCHANGE_NAME)
                .withArgument("x-dead-letter-routing-key",DELAY_PROCESS_QUEUE_NAME)
               // .withArgument("x-message-ttl",DELAY_EXPIRATION)
                .build();
    }

    //创建一个工作队列
    @Bean
    Queue delayProcessQueue(){
        return QueueBuilder.durable(DELAY_PROCESS_QUEUE_NAME).build();
    }

    //创建一个死信交换机
    @Bean
    DirectExchange delayExchange(){
        return new DirectExchange(DELAY_EXCHANGE_NAME);
    }
    //创建绑定关系
    @Bean
    Binding dlxBinding(Queue delayProcessQueue, DirectExchange delayExchange){
        return BindingBuilder.bind(delayProcessQueue)
                .to(delayExchange).with(DELAY_PROCESS_QUEUE_NAME);
    }

}
